﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using System.Linq.Expressions;
namespace BAL
{
    public class StudentScoreExtend : BaseBAL<StudentScoreExtendInfo, Models.StudentScoreExtend>
    {
        private static string CacheName = "AllStudentScoreExtendInfos";

        public static IList<StudentScoreExtendInfo> GetList()
        {
            if (!CacheHelper.IsExist(CacheName))
            {
                ToCache();
            }
            IList<StudentScoreExtendInfo> studentScoreExtendInfos = (IList<StudentScoreExtendInfo>)CacheHelper.GetCache(CacheName);
            return studentScoreExtendInfos;
        }

        public static void ToCache()
        {
            
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Models.StudentScoreExtend> studentScoreExtendInfos = db.StudentScoreExtends.ToList();
                IList<StudentScoreExtendInfo> infos = studentScoreExtendInfos.Select(t => TransT2ToT1(t)).ToList();
                CacheHelper.Refurbish(CacheName, infos, 0, 60);
            }
        }

        public static IList<StudentScoreExtendInfo> GetList(int roomid, int areaid, int schoolid, int gradeid, int classid, int studentid, int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    return db.GetTable<Models.StudentScoreExtend>().Where(GetExpression(roomid, areaid, schoolid, gradeid, classid, studentid)).Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.StudentScoreExtend>().Where(GetExpression(roomid, areaid, schoolid, gradeid, classid, studentid)).Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }
        public static int GetCount(int roomid, int areaid, int schoolid, int gradeid, int classid,int studentid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.StudentScoreExtend>().Where(GetExpression(roomid, areaid, schoolid, gradeid, classid,studentid)).Count();
            }
        }

        public static decimal GetScoreCount(int roomid, int areaid, int schoolid, int gradeid, int classid, int studentid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var query= db.GetTable<Models.StudentScoreExtend>().Where(GetExpression(roomid, areaid, schoolid, gradeid, classid, studentid));
                if (query.Count() > 0)
                {
                    return query.Sum(t => t.CountScore);
                }
                else
                {
                    return 0;
                }
            }
        }
        private static Expression<Func<Models.StudentScoreExtend, bool>> GetExpression(int roomId, int areaId, int schoolid, int gradeid, int classid,int studentid)
        {
            var predicate = PredicateBuilder.True<Models.StudentScoreExtend>();
            if (roomId > 0)
                predicate = predicate.And(p => p.ExamRoomId== roomId);
            if (areaId > 0)
                predicate = predicate.And(p => p.AreaId== areaId);
            if (schoolid > 0)
                predicate = predicate.And(p => p.SchoolId== schoolid);
            if (gradeid > 0)
                predicate = predicate.And(p => p.GradeId == gradeid);
            if (classid > 0)
                predicate = predicate.And(p => p.ClassId == classid);
            if(studentid > 0)
                predicate = predicate.And(p => p.StudentId == studentid);
            return predicate;
        }

        public static StudentScoreExtendInfo GetSingleInfo(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransT2ToT1(db.StudentScoreExtends.Single(t => t.Id == id));
            }
        }
    }
}
